﻿Imports System
Imports System.Web.UI
Imports System.Web.UI.Design
Imports System.Web.UI.WebControls
Imports System.ComponentModel
Imports System.Drawing
Imports System.Drawing.Design

Namespace Controls
    ''' <summary> 
    ''' Popup window anchor control 
    ''' </summary> 
    <DefaultProperty("PopupToShow"), ToolboxData("<{0}:PopupWinAnchor runat=server></{0}:PopupWinAnchor>")> _
    <Designer(GetType(AnchorDesigner))> _
    Public Class PopupWinAnchor
        Inherits System.Web.UI.WebControls.WebControl
#Region "Variables"

        Private controlId As String, controlLink As String, jsEvent As String
        Private snMsg As String, snText As String, snTitle As String
        Private bChangeText As Boolean = False

#End Region
#Region "Constructor"

        ''' <summary> 
        ''' Create control 
        ''' </summary> 
        Public Sub New()
            jsEvent = "onclick"
            bChangeText = False
        End Sub

#End Region
#Region "Properties"

        ''' <summary> 
        ''' Should texts on PopupWin be replaced with new texts ? 
        ''' </summary> 
        <Bindable(True), Category("PopupWin"), DefaultValue(False)> _
        <Description("Should texts on PopupWin be replaced with new texts ?")> _
        Public Property ChangeTexts() As Boolean
            Get
                Return bChangeText
            End Get
            Set(ByVal value As Boolean)
                bChangeText = value
            End Set
        End Property


        ''' <summary> 
        ''' New message text 
        ''' </summary> 
        <Bindable(True), Category("PopupWin"), DefaultValue("")> _
        <Description("New message text")> _
        Public Property NewMessage() As String
            Get
                Return snMsg
            End Get
            Set(ByVal value As String)
                snMsg = value
            End Set
        End Property


        ''' <summary> 
        ''' New popup title text 
        ''' </summary> 
        <Bindable(True), Category("PopupWin"), DefaultValue("")> _
        <Description("New popup title text")> _
        Public Property NewTitle() As String
            Get
                Return snTitle
            End Get
            Set(ByVal value As String)
                snTitle = value
            End Set
        End Property


        ''' <summary> 
        ''' New text to show in opened window 
        ''' </summary> 
        <Bindable(True), Category("PopupWin"), DefaultValue("")> _
        <Description("New text to show in opened window")> _
        Public Property NewText() As String
            Get
                Return snText
            End Get
            Set(ByVal value As String)
                snText = value
            End Set
        End Property


        ''' <summary> 
        ''' JavaScript event to handle 
        ''' </summary> 
        <Bindable(True), Category("Anchor"), DefaultValue("onclick")> _
        <Editor(GetType(JavaScriptEventEditor), GetType(UITypeEditor))> _
        <Description("JavaScript event to handle")> _
        Public Property HandledEvent() As String
            Get
                Return jsEvent
            End Get
            Set(ByVal value As String)
                jsEvent = value
            End Set
        End Property


        ''' <summary> 
        ''' Popup control to show when event occurs 
        ''' </summary> 
        <Bindable(True), Category("Anchor"), DefaultValue("")> _
        <Editor(GetType(PopupControlsEditor), GetType(UITypeEditor))> _
        <Description("Popup control to show when event occurs")> _
        Public Property PopupToShow() As String
            Get
                Return controlId
            End Get
            Set(ByVal value As String)
                controlId = value
            End Set
        End Property


        ''' <summary> 
        ''' Contol which event will cause the popup to show 
        ''' </summary> 
        <Bindable(True), Category("Anchor"), DefaultValue("")> _
        <Editor(GetType(AllControlsEditor), GetType(UITypeEditor))> _
        <Description("Control ")> _
        Public Property LinkedControl() As String
            Get
                Return controlLink
            End Get
            Set(ByVal value As String)
                controlLink = value
            End Set
        End Property

#End Region
#Region "Methods"

        ''' <summary> 
        ''' Render script for displaying popup control 
        ''' </summary> 
        ''' <param name="output">The HTML writer to write out to</param> 
        Protected Overloads Overrides Sub Render(ByVal output As HtmlTextWriter)
            output.Write("" & Chr(13) & "" & Chr(10) & " <script type=""text/javascript"">" & Chr(13) & "" & Chr(10) & " //<![CDATA[" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & " var " + ID + "oldOnLoad=window.onload;" & Chr(13) & "" & Chr(10) & " window.onload=" + ID + "espopup_anchorInit;" & Chr(13) & "" & Chr(10) & " function " + ID + "espopup_anchorInit()" & Chr(13) & "" & Chr(10) & " {" & Chr(13) & "" & Chr(10) & " if (" + ID + "oldOnLoad!=null) " + ID + "oldOnLoad();" & Chr(13) & "" & Chr(10) & " document.getElementById('" + controlLink + "')." + jsEvent + "=" + ID + "espopup_anchorEvent;" & Chr(13) & "" & Chr(10) & " }" & Chr(13) & "" & Chr(10) & "" & Chr(13) & "" & Chr(10) & " function " + ID + "espopup_anchorEvent()" & Chr(13) & "" & Chr(10) & " {" & Chr(13) & "" & Chr(10) & " ")
            If bChangeText Then
                Dim ct As Control = Page.FindControl(controlId)
                If ct IsNot Nothing Then
                    output.Write(controlId + "nText=""" + DirectCast(ct, PopupWin).GetWinText(snTitle, snText) + """;" & Chr(10) & "")
                End If
                output.Write(controlId + "nMsg=""" + snMsg + """;" & Chr(10) & "")
                output.Write(controlId + "nTitle=""" + snTitle + """;" & Chr(10) & "")
                output.Write(controlId + "bChangeTexts=true;" & Chr(10) & "")
            Else
                output.Write(controlId + "bChangeTexts=false;" & Chr(10) & "")
            End If
            output.Write("" & Chr(10) & "" + controlId + "espopup_ShowPopup('" + ID + "');" & Chr(13) & "" & Chr(10) & " }" & Chr(13) & "" & Chr(10) & " //]]>" & Chr(13) & "" & Chr(10) & " </script>")
        End Sub

#End Region
    End Class


    ''' <summary> 
    ''' Class for displaying PopupWin in designer 
    ''' </summary> 
    Public Class AnchorDesigner
        Inherits ControlDesigner
#Region "Overriden methods"

        ''' <summary> 
        ''' Returns HTML code to show in designer 
        ''' </summary> 
        Public Overloads Overrides Function GetDesignTimeHtml() As String
            Return "<div style=""padding:2px; background-color: ButtonFace;color:ButtonText; " + "border-style:outset; border-width:1px; font: 75% 'Microsoft Sans Serif';""><b>" + "PopupWinAnchor</b> - " + DirectCast(Component, Control).ID + "</div>"
        End Function

#End Region
    End Class


    ''' <summary> 
    ''' Editor for selecting JavaScript event 
    ''' </summary> 
    Public Class JavaScriptEventEditor
        Inherits UITypeEditor
#Region "Variables"

        Private edSvc As System.Windows.Forms.Design.IWindowsFormsEditorService = Nothing
        Private lb As System.Windows.Forms.ListBox

#End Region
#Region "Methods"

        ''' <summary> 
        ''' Overrides the method used to provide basic behaviour for selecting editor. 
        ''' Shows our custom control for editing the value. 
        ''' </summary> 
        ''' <param name="context">The context of the editing control</param> 
        ''' <param name="provider">A valid service provider</param> 
        ''' <param name="value">The current value of the object to edit</param> 
        ''' <returns>The new value of the object</returns> 
        Public Overloads Overrides Function EditValue(ByVal context As ITypeDescriptorContext, ByVal provider As IServiceProvider, ByVal value As Object) As Object
            If context IsNot Nothing AndAlso context.Instance IsNot Nothing AndAlso provider IsNot Nothing Then
                edSvc = DirectCast(provider.GetService(GetType(System.Windows.Forms.Design.IWindowsFormsEditorService)), System.Windows.Forms.Design.IWindowsFormsEditorService)
                If edSvc IsNot Nothing Then
                    lb = New System.Windows.Forms.ListBox()
                    lb.BorderStyle = System.Windows.Forms.BorderStyle.None
                    AddHandler lb.SelectedIndexChanged, AddressOf lb_SelectedIndexChanged
                    lb.Items.Add("onclick")
                    lb.Items.Add("ondblclick")
                    lb.Items.Add("onmouseover")
                    lb.Items.Add("onfocus")
                    lb.Items.Add("oncontextmenu")
                    edSvc.DropDownControl(lb)
                    If lb.SelectedIndex = -1 Then
                        Return value
                    End If
                    Return lb.SelectedItem
                End If
            End If

            Return value
        End Function


        ''' <summary> 
        ''' Choose editor type 
        ''' </summary> 
        ''' <param name="context">The context of the editing control</param> 
        ''' <returns>Returns <c>UITypeEditorEditStyle.DropDown</c></returns> 
        Public Overloads Overrides Function GetEditStyle(ByVal context As ITypeDescriptorContext) As UITypeEditorEditStyle
            Return UITypeEditorEditStyle.DropDown
        End Function


        ''' <summary> 
        ''' Close the dropdowncontrol when the user has selected a value 
        ''' </summary> 
        Private Sub lb_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
            If edSvc IsNot Nothing Then
                edSvc.CloseDropDown()
            End If
        End Sub

#End Region
    End Class


    ''' <summary> 
    ''' Editor for selecting controls from Asp.Net page 
    ''' </summary> 
    Public MustInherit Class ControlsEditor
        Inherits UITypeEditor
#Region "Variables"

        Private edSvc As System.Windows.Forms.Design.IWindowsFormsEditorService = Nothing
        Private lb As System.Windows.Forms.ListBox
        Private typeShow As Type

#End Region
#Region "Constructor"


        ''' <summary> 
        ''' onstructor - show specified types 
        ''' </summary> 
        ''' <param name="show">Type descriptor</param> 
        Public Sub New(ByVal show As Type)
            typeShow = show
        End Sub

#End Region
#Region "Methods"

        ''' <summary> 
        ''' Overrides the method used to provide basic behaviour for selecting editor. 
        ''' Shows our custom control for editing the value. 
        ''' </summary> 
        ''' <param name="context">The context of the editing control</param> 
        ''' <param name="provider">A valid service provider</param> 
        ''' <param name="value">The current value of the object to edit</param> 
        ''' <returns>The new value of the object</returns> 
        Public Overloads Overrides Function EditValue(ByVal context As ITypeDescriptorContext, ByVal provider As IServiceProvider, ByVal value As Object) As Object
            If context IsNot Nothing AndAlso context.Instance IsNot Nothing AndAlso provider IsNot Nothing Then
                edSvc = DirectCast(provider.GetService(GetType(System.Windows.Forms.Design.IWindowsFormsEditorService)), System.Windows.Forms.Design.IWindowsFormsEditorService)
                If edSvc IsNot Nothing Then
                    lb = New System.Windows.Forms.ListBox()
                    lb.BorderStyle = System.Windows.Forms.BorderStyle.None
                    AddHandler lb.SelectedIndexChanged, AddressOf lb_SelectedIndexChanged
                    For Each ctrl As Control In DirectCast(context.Instance, Control).Page.Controls
                        If ctrl.[GetType]().IsSubclassOf(typeShow) OrElse ctrl.[GetType]().FullName = typeShow.FullName Then
                            lb.Items.Add(ctrl.ID)
                        End If
                    Next
                    edSvc.DropDownControl(lb)
                    If lb.SelectedIndex = -1 Then
                        Return value
                    End If
                    Return lb.SelectedItem
                End If
            End If

            Return value
        End Function


        ''' <summary> 
        ''' Choose editor type 
        ''' </summary> 
        ''' <param name="context">The context of the editing control</param> 
        ''' <returns>Returns <c>UITypeEditorEditStyle.DropDown</c></returns> 
        Public Overloads Overrides Function GetEditStyle(ByVal context As ITypeDescriptorContext) As UITypeEditorEditStyle
            Return UITypeEditorEditStyle.DropDown
        End Function


        ''' <summary> 
        ''' Close the dropdowncontrol when the user has selected a value 
        ''' </summary> 
        Private Sub lb_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
            If edSvc IsNot Nothing Then
                edSvc.CloseDropDown()
            End If
        End Sub

#End Region
    End Class


    ''' <summary> 
    ''' Editor for selecting all Asp.Net controls 
    ''' </summary> 
    Public Class AllControlsEditor
        Inherits ControlsEditor
#Region "Members"

        ''' <summary> 
        ''' Invoke base constructor 
        ''' </summary> 
        Public Sub New()
            MyBase.New(GetType(Control))
        End Sub

#End Region
    End Class


    ''' <summary> 
    ''' Editor for selecting PopupWin controls 
    ''' </summary> 
    Public Class PopupControlsEditor
        Inherits ControlsEditor
#Region "Members"

        ''' <summary> 
        ''' Invoke base constructor 
        ''' </summary> 
        Public Sub New()
            MyBase.New(GetType(PopupWin))
        End Sub

#End Region
    End Class
End Namespace